/**
 * 
 */
package com.edocbase.desktop.plugins.mdiary.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Figure;

import com.edocbase.biz.diary.entities.WebAccount;
import com.edocbase.desktop.maildriver.DesktopApplication;
import com.edocbase.desktop.maildriver.DriverWorker;
import com.edocbase.desktop.plugins.mdairy.part.diary.DiaryWorkPart;
import com.edocbase.desktop.plugins.mdairy.part.diary.SelecteAccountDialog;
import com.edocbase.webdriver.DriverException;

/**
 * @author lifei114@gmail.com
 * 
 */
public class UpDownAction implements ActionListener {
	org.apache.log4j.Logger logger = org.apache.log4j.Logger
			.getLogger(UpDownAction.class);

	private DiaryWorkPart part = null;
	private String action = null;
	private Figure parent = null;

	public UpDownAction(DiaryWorkPart part, String action, Figure parent) {
		super();
		this.part = part;
		this.action = action;
		this.parent = parent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.draw2d.ActionListener#actionPerformed(org.eclipse.draw2d.
	 * ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		if (logger.isDebugEnabled()) {
			logger.debug("action=" + action);
		}
		SelecteAccountDialog dialog = new SelecteAccountDialog(part.getViewer()
				.getDesktopShell().getShell(), parent);
		Object obj = dialog.open();
		if (obj != null) {
			List<WebAccount> actList = (List<WebAccount>) obj;
			if (action.equals("up")) {
				doUp(actList);
			} else if (action.equals("down")) {
				doDown(actList);
			}
		}
	}

	private void doUp(List<WebAccount> actList) {
		List<String> accounts = new ArrayList<String>();
		for (WebAccount account:actList){
			accounts.add(account.getFullAccount());
			DriverWorker worker = DriverWorker.getWorker();
		
		}
		DesktopApplication.getPropertyConfig().setProperty("last.up.account", accounts);
		
		
		
	}

	private void doDown(List<WebAccount> actList) {
		List<String> accounts = new ArrayList<String>();
		for (WebAccount account:actList){
			accounts.add(account.getFullAccount());
			DriverWorker worker = DriverWorker.getWorker();
			try {
				worker.receive(account);
			} catch (DriverException e) {
				e.printStackTrace();
			}
		}
		DesktopApplication.getPropertyConfig().setProperty("last.down.account", accounts);

	}
}
